﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xant.FlowEngine.Interface;

namespace Xant.FlowEngine.Core
{
    public sealed class WorkActivity : ManualActivity
    {
        #region 构造函数
        public WorkActivity(): this(null, null)
        {
        }

        public WorkActivity(string code): this(code, null)
        {
        }

        public WorkActivity(string code, string name)
            : base(code, name)
        {
        }

        #endregion

        #region 实现抽象方法
        protected override AbstractActivity Clone()
        {
            var clone = new WorkActivity(this.Code, this.Name);
            //to do:将本类声明的字段、属性作克隆处理，父类声明的由父类处理
            return clone;
        }

        #endregion

        #region 公有方法
        public void Init()
        {
        }

        public override void Entry()
        {
            base.Entry();
            //处理活动节点逻辑，生成相应的工作项
            List<Workitem> list = new List<Workitem>();
            list.Add(new Workitem());

            //将工作项添加到活动工作项集合中，并触发WorkitemInitialized事件
            var items = list.ToArray();
            AddWorkitem(items);
            if (WorkitemInitialized != null)
            {
                WorkitemInitialized(this);
            }
        }

        #endregion


        #region 事件响应
        protected override void workitem_AfterAction(AbstractWorkitem workitem)
        {
            //在此用规则引擎判断是否应对此活动作出状态更新。
            //例如：审批人数是否达到阀值，驳回者是否具有一票否决权等等
            SerializerManager.Save(workitem);
            switch(workitem.Action)
            {
                case WorkAction.Pass:
                    this.Leave();
                    break;
                default:
                    throw new NotImplementedException();
            }
        }

        #endregion

    }
}
